﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gmis.IDAL;
using System.Data.SqlClient;
using System.Data;
using Gmis.DBUtility;

namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:Village
    /// </summary>
    public partial class Village : IVillage
    {
        public Village()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.Village model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Village(");
            strSql.Append("sId,sName,sShort,sArea,sAddress,nSort,sDescription,lEnable,sComment,dCreate)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sName,@sShort,@sArea,@sAddress,@nSort,@sDescription,@lEnable,@sComment,@dCreate)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sName", SqlDbType.VarChar,50),
					new SqlParameter("@sShort", SqlDbType.VarChar,50),
					new SqlParameter("@sArea", SqlDbType.VarChar,50),
					new SqlParameter("@sAddress", SqlDbType.NVarChar,260),
					new SqlParameter("@nSort", SqlDbType.Int,4),
					new SqlParameter("@sDescription", SqlDbType.NText),
					new SqlParameter("@lEnable", SqlDbType.Bit,1),
					new SqlParameter("@sComment", SqlDbType.VarChar,300),
					new SqlParameter("@dCreate", SqlDbType.DateTime)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sName;
            parameters[2].Value = model.sShort;
            parameters[3].Value = model.sArea;
            parameters[4].Value = model.sAddress;
            parameters[5].Value = model.nSort;
            parameters[6].Value = model.sDescription;
            parameters[7].Value = model.lEnable;
            parameters[8].Value = model.sComment;
            parameters[9].Value = model.dCreate;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.Village model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Village set ");
            strSql.Append("sName=@sName,");
            strSql.Append("sShort=@sShort,");
            strSql.Append("sArea=@sArea,");
            strSql.Append("sAddress=@sAddress,");
            strSql.Append("nSort=@nSort,");
            strSql.Append("sDescription=@sDescription,");
            strSql.Append("lEnable=@lEnable,");
            strSql.Append("sComment=@sComment,");
            strSql.Append("dCreate=@dCreate");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sName", SqlDbType.VarChar,50),
					new SqlParameter("@sShort", SqlDbType.VarChar,50),
					new SqlParameter("@sArea", SqlDbType.VarChar,50),
					new SqlParameter("@sAddress", SqlDbType.NVarChar,260),
					new SqlParameter("@nSort", SqlDbType.Int,4),
					new SqlParameter("@sDescription", SqlDbType.NText),
					new SqlParameter("@lEnable", SqlDbType.Bit,1),
					new SqlParameter("@sComment", SqlDbType.VarChar,300),
					new SqlParameter("@dCreate", SqlDbType.DateTime),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sName;
            parameters[1].Value = model.sShort;
            parameters[2].Value = model.sArea;
            parameters[3].Value = model.sAddress;
            parameters[4].Value = model.nSort;
            parameters[5].Value = model.sDescription;
            parameters[6].Value = model.lEnable;
            parameters[7].Value = model.sComment;
            parameters[8].Value = model.dCreate;
            parameters[9].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Village ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Village ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.Village GetModel(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sId,sName,sShort,sArea,sAddress,nSort,sDescription,lEnable,sComment,dCreate from Village ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.Village model = new Gmis.Model.Village();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sShort"] != null && ds.Tables[0].Rows[0]["sShort"].ToString() != "")
                {
                    model.sShort = ds.Tables[0].Rows[0]["sShort"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sArea"] != null && ds.Tables[0].Rows[0]["sArea"].ToString() != "")
                {
                    model.sArea = ds.Tables[0].Rows[0]["sArea"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sAddress"] != null && ds.Tables[0].Rows[0]["sAddress"].ToString() != "")
                {
                    model.sAddress = ds.Tables[0].Rows[0]["sAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nSort"] != null && ds.Tables[0].Rows[0]["nSort"].ToString() != "")
                {
                    model.nSort = int.Parse(ds.Tables[0].Rows[0]["nSort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sDescription"] != null && ds.Tables[0].Rows[0]["sDescription"].ToString() != "")
                {
                    model.sDescription = ds.Tables[0].Rows[0]["sDescription"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lEnable"] != null && ds.Tables[0].Rows[0]["lEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["lEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["lEnable"].ToString().ToLower() == "true"))
                    {
                        model.lEnable = true;
                    }
                    else
                    {
                        model.lEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["sComment"] != null && ds.Tables[0].Rows[0]["sComment"].ToString() != "")
                {
                    model.sComment = ds.Tables[0].Rows[0]["sComment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dCreate"] != null && ds.Tables[0].Rows[0]["dCreate"].ToString() != "")
                {
                    model.dCreate = DateTime.Parse(ds.Tables[0].Rows[0]["dCreate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sId,sName,sShort,sArea,sAddress,nSort,sDescription,lEnable,sComment,dCreate ");
            strSql.Append(" FROM Village ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sId,sName,sShort,sArea,sAddress,nSort,sDescription,lEnable,sComment,dCreate ");
            strSql.Append(" FROM Village ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Village";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
